﻿using BellAssistant.Common.CommonHelper;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BellAssistant.Model.Tables;

namespace BellAssistant.DSS
{
 
    public class DataSourceService :IDataSourceService 
    {
        #region base
        /// <summary>
        /// 构造器
        /// </summary>
        private DataSourceService() { }

        public static IDataSourceService GetDataSourceService()
        {
            return new DataSourceService();
        }

        /// <summary>
        /// 接口地址
        /// </summary>
        public static string ApiUrl
        {
            get
            {
                string apiUrl = ConfigurationManager.AppSettings["Bell_APIUrl"];
                return apiUrl;
            }
        }

       

        /// <summary>
        /// 建立API接口,返回object
        /// </summary>
        /// <param name="objModel">参数</param>
        /// <returns></returns>
        public object bulidAPI(string controller, string action, object objModel)
        {
            try
            {
                string url = string.Format("{0}/{1}/{2}", ApiUrl, controller, action);
                Task<object> objReturn = APIHelper.GetJsonData(url, objModel);
                if (objReturn != null)
                {
                    if (!objReturn.IsCanceled)
                    {
                        if (objReturn.Result != null)
                        {
                            //return JsonConvert.DeserializeObject<T>(objReturn.Result.ToString());
                            return objReturn.Result;
                        }
                        LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn.Result为Null ");
                    }
                    LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn被取消 ");
                }
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn为Null ");
                return null;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常 " + e.Message, e);
                return null;
            }
        }

        /// <summary>
        /// 建立API接口,返回string
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <param name="objModel"></param>
        /// <returns></returns>
        public string bulidAPIString(string controller, string action, object objModel)
        {
            try
            {
                string url = string.Format("{0}/{1}/{2}", ApiUrl, controller, action);
                Task<object> objReturn = APIHelper.GetJsonData(url, objModel);
                if (objReturn != null)
                {
                    if (!objReturn.IsCanceled)
                    {
                        if (objReturn.Result != null)
                        {
                            return objReturn.Result.ToString();
                        }
                        LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn.Result为Null ");
                    }
                    LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn被取消 ");
                }
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn为Null ");
                return null;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常 " + e.Message, e);
                return null;
            }
        }

        /// <summary>
        /// 建立API接口,返回T (T可以为List<T>)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controller">SSCMS.Web.API项目控制器controller的名称</param>
        /// <param name="action">SSCMS.Web.API项目的action名称</param>
        /// <param name="objModel">接口参数</param>
        /// <returns></returns>
        public T bulidAPI<T>(string controller, string action, object objModel)
        {
            try
            {
                string url = string.Format("{0}/{1}/{2}", ApiUrl, controller, action);
                Task<object> objReturn = APIHelper.GetJsonData(url, objModel);
                if (objReturn != null)
                {
                    if (!objReturn.IsCanceled)
                    {
                        if (objReturn.Result != null)
                        {
                            return JsonConvert.DeserializeObject<T>(objReturn.Result.ToString());
                        }
                        LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn.Result为Null ");
                    }
                    LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn被取消 ");
                }
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn为Null ");
                return default(T);
            }
            catch (Exception e)
            {
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常 " + e.Message, e);
                return default(T);
            }
        }

        /// <summary>
        /// 建立API接口,返回List<T>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="controller">SSCMS.Web.API项目控制器controller的名称</param>
        /// <param name="action">SSCMS.Web.API项目的action名称</param>
        /// <param name="objModel">接口参数</param>
        /// <returns></returns>
        public List<T> bulidAPI_L<T>(string controller, string action, object objModel)
        {
            try
            {
                string url = string.Format("{0}/{1}/{2}", ApiUrl, controller, action);
                Task<object> objReturn = APIHelper.GetJsonData(url, objModel);
                if (objReturn != null)
                {
                    if (!objReturn.IsCanceled)
                    {
                        if (objReturn.Result != null)
                        {
                            return JsonConvert.DeserializeObject<List<T>>(objReturn.Result.ToString());
                        }
                        LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn.Result为Null ");
                    }
                    LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn被取消 ");
                }
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常：接口返回的objReturn为Null ");
                return null;
            }
            catch (Exception e)
            {
                LogHelper.WriteLog.Error(LogHelper.GetFatherMethodName() + string.Format(" 调用后台服务端：[{0}] [{1}]", controller, action) + " 时出现异常 " + e.Message, e);
                return null;
            }
        }


        #endregion

        public bool SaveGoods(Goods good)
        {
            return (bool)bulidAPI("Data", "SaveGoods", new { good= good });
        }

        public List<Goods> GetAllGoods()
        {
            return bulidAPI<List<Goods>>("Data", "GetAllGoods", new { });
        }

        public bool SaveCategory(ProductCategory product, string newName)
        {
            return (bool)bulidAPI("Data", "SaveCategory", new { product = product, newName = newName });
        }

        public  List<ProductCategory> GetAllProductCategory()
        {
            return bulidAPI<List<ProductCategory>>("Data", "GetAllProductCategory", new {});
        }

        public  string SaveBrand(string brandName)
        {
            return (string)bulidAPI("Data", "SaveBrand", new { brandName = brandName });
        }

        public List<Brand> GetAllBrand()
        {
            return bulidAPI<List<Brand>>("Data", "GetAllBrand", new { });
        }

        public string SaveExpressType(ExpressType expressType)
        {
            return (string)bulidAPI("Data", "SaveExpressType", new { expressType = expressType });
        }

        public List<ExpressType> GetAllExpressType()
        {
            return bulidAPI<List<ExpressType>>("Data", "GetAllExpressType", new { });
        }

        public List<PromotionType> GetAllPromotionType()
        {
            return bulidAPI<List<PromotionType>>("Data", "GetAllPromotionType", new { });
        }

        public string SavePromotionType(PromotionType promotionType)
        {
            return (string)bulidAPI("Data", "SavePromotionType", new { promotionType = promotionType });
        }


        public int GetProductCategoryIDByName(string categoryName)
        {
            return bulidAPI<int>("Data", "GetProductCategoryIDByName", new { categoryName = categoryName });
        }

        public int GetBrandIDByName(string brandName)
        {
            return bulidAPI<int>("Data", "GetBrandIDByName", new { brandName = brandName });
        }

        public int GetExpressTypeIDByName(string expressTypeName)
        {
            return bulidAPI<int>("Data", "GetExpressTypeIDByName", new { expressTypeName = expressTypeName });
        }

        public int GetPromotionIDByName(string promotionName)
        {
            return bulidAPI<int>("Data", "GetPromotionIDByName", new { promotionName = promotionName });
        }

        public bool DelProductCategory(int id)
        {
            return (bool)bulidAPI("Data", "DelProductCategory", new { id = id });
        }
    }
}
